// pages/school/school.js
const util = require("../../utils/util.js");
const app = getApp()
const baseImgUrl = util.baseImgUrl;
const jumpurl = util.jumpurl;
var page = 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showGrey: false,
    wb_bgImg: baseImgUrl + "wb_bgImg.png",
    scanPic:baseImgUrl+"scan_home.png",
  },
  /********选择分类************* */
  choseNav:function(e){
    var arr="";
    var that=this;
      var index=e.currentTarget.dataset.index;
    var subindex = e.currentTarget.dataset.subindex;
    var nameid = e.currentTarget.dataset.nameid;
      console.log(index+"******"+subindex);
    var len = that.data.classes.length;
    //循环数组
    for(var i=0;i<len;i++){
      if(index==i){
        var subLen = that.data.classes[i].listname.length;
        for(var j=0;j<subLen;j++){
          that.data.classes[i].listname[j].tag=0;
         }
         that.data.classes[i].listname[subindex].tag=1;
        }
      }
      //循环结束
      that.setData({
        classes: that.data.classes
      })

    for (var i = 0; i < len; i++) {
      var subLen = that.data.classes[i].listname.length;
      for (var j = 0; j < subLen; j++) {
        if (that.data.classes[i].listname[j].tag ==1){
          arr+= that.data.classes[i].listname[j].nameid+"|";
        }
      }
    }
    that.setData({
      arr_list: arr,
    })
    wx.request({
      url: jumpurl + 'index/get_video',
      data: {
        classid: arr,
        yijiid:that.data.yijiid,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res);
        that.setData({
          newestlist: res.data.newestlist,
        });
      },
    })  
   
  },
  //点击视频
  audiodetail: function (e) {
    var that = this;
    var newid = e.currentTarget.dataset.id;
    var levels = that.data.levels;
    wx.request({
      url: jumpurl + 'my/get_vip',
      data: {
        openid: app.globalData.openid,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.vip_level != levels) {
          that.setData({
            showGrey: true,
          })
        } else {
          wx.navigateTo({
            url: '/pages/audioDetail/audioDetail?newid=' + newid,
          })
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: jumpurl + 'index/class_info',
      data: {
        classid: options.classid,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res);
        that.setData({
          classes: res.data.classes,
          newestlist: res.data.newestlist,
          yijiid: options.classid,
          arr_list: res.data.class_list,
          wbText: res.data.wbText, 
          levels: res.data.levels, 
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //到底
  onReachBottom: function () {
    util.loading();
    //请求接口
    var that = this;
    var newestlist = that.data.newestlist;
    var arr_list = that.data.arr_list;
    page = page + 1;
    wx.request({
      url: jumpurl + 'index/class_info_page',
      data: {
        page: page,
        class_list: arr_list,
        yijiid:that.data.yijiid,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res);
        if (page <= res.data.zongye) {
          newestlist = newestlist.concat(res.data.newestlist);
          that.setData({
            newestlist: newestlist
          })
        }
        if (page > res.data.zongye) {
          wx.showToast({
            icon: "none",
            title: '没有更多数据了',
          })
        }
      },
    })
  },

  ///我再想想
  wait: function () {
    var that = this;
    that.setData({
      showGrey: false
    })
  },
  //开通会员
  member: function () {
    var that = this;
    that.setData({
      showGrey: false
    })
    wx.navigateTo({
      url: '/pages/kaitong/kaitong',
    })
  },
})